package com.cqut.hfh.ordering.service.Impl;

import com.cqut.hfh.ordering.pojo.EhyManager;
import com.cqut.hfh.ordering.pojo.EhyManagerRole;
import com.cqut.hfh.ordering.service.IManagerService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.li.mybatisutil.dao.base.Dao;
import com.cqut.li.mybatisutil.dao.base.EntityDao;
import com.cqut.li.mybatisutil.dao.base.SearchDao;
import com.cqut.li.mybatisutil.entity.base.Entity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author LJH
 * @date 2019/6/22-19:51
 */
@Service
public class ManagerServiceImpl implements IManagerService {

    @Resource(name = "Dao")
    private Dao dao;
    @Resource
    private SearchDao searchDao;

    @Override
    public EhyManager login(String man_user, String man_pwd) {

        String condition = " man_user = '" + man_user + "'";

        List<Map<String, Object>> temp_map = dao.findByCondition(new String[]{
                "man_pwd",
                "man_user",
                "man_id"},
                condition,
                EhyManager.class);

        if(temp_map == null) {
            return null;
        }

        for( Map<String, Object> map : temp_map ){
            //密码判断
            if(map.get("man_pwd").toString().equals(man_pwd)){

                EhyManager ehyManager = new EhyManager();
                ehyManager.setProperties(map);
                return ehyManager;
            }
        }

        return null;
    }

    @Override
    public List<Map<String, Object>> getManage(String limit, String page) {
        String con = " 1 = 1 limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        List<Map<String, Object>> manage_list
                = dao.findByCondition(new String[]{"man_id","st_id",
                        "man_user",
                        "man_pwd",
                        "man_phone",
                        "man_cardid",
                        "man_remark",
                        "optime",
                        "oper",
                        "isva"},
                con,
                EhyManager.class);

        return manage_list;
    }

    @Override
    public int saveManage(EhyManager manager, String role_ids) {
        int save = dao.save(manager);
        String[] role_strs = role_ids.split(",");
        for( String role_str : role_strs ){
            EhyManagerRole mr = new EhyManagerRole();
            mr.setMan_id(manager.getMan_id());
            mr.setMr_id(EntityIdutils.getId());
            mr.setRole_id(role_str);
            dao.save(mr);
        }
        return save;
    }

    @Override
    public int updateManage(EhyManager manager, String role_ids) {

        String[] role_strs = role_ids.split(",");
        int a = dao.updatePropByID(manager, manager.getMan_id());
        dao.deleteByCondition(" man_id='" + manager.getMan_id() + "'", EhyManagerRole.class);
        for( String role_str : role_strs ){
            EhyManagerRole mr = new EhyManagerRole();
            mr.setMan_id(manager.getMan_id());
            mr.setMr_id(EntityIdutils.getId());
            mr.setRole_id(role_str);
            dao.save(mr);
        }
        return 1;
    }

    @Override
    public Integer getCount() {
        return searchDao.sqlCount(Entity.getTableName(EhyManager.class));
    }

    @Override
    public int delById(String id) {
        int a = dao.deleteByID(id, EhyManager.class);
        int b = delManagerRoleById(id);
        return a+b;
    }

    public int delManagerRoleById(String id){
        return dao.deleteByCondition(" man_id = '" + id + "'", EhyManagerRole.class);
    }

    @Override
    public Map<String, Object> findById(String id) {
        return dao.getByID(id, EhyManager.class).toMap();
    }

    @Override
    public List<Map<String, Object>> getManagersByName(String limit, String page, String man_user) {
        String con = " man_user like '%" + man_user + "%' limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        List<Map<String, Object>> managers_list
                = dao.findByCondition(new String[]{"man_id","st_id",
                        "man_user",
                        "man_pwd",
                        "man_phone",
                        "man_cardid",
                        "man_remark",
                        "optime",
                        "oper",
                        "isva"},
                con,
                EhyManager.class);

        return managers_list;
    }


}
